﻿using System;
using System.Drawing;
using System.Web;
using System.Web.Security;

using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;
using System.Diagnostics;

namespace SLIITPro.Controls
{
    public partial class Widget : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                InitializeComponent();
        }

        private void InitializeComponent()
        {
            lblRating.Text = RatingText;
            RatingBusiness.CurrentRating = RatingValue;
            lnkWishList.Text = GetLinkText();
            lnkWishList.ForeColor = GetLinkColor();
        }

        private int _ratingValue;
        public int RatingValue
        {
            get { return _ratingValue; }
            set { _ratingValue = value; }
        }

        private string _ratingText;
        public string RatingText
        {
            get { return _ratingText; }
            set { _ratingText = value; }
        }

        private int _userId;
        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        private int _businessId;
        public int BusinessId
        {
            get { return _businessId; }
            set { _businessId = value; }
        }

        private BusinessItemExtension _itemExtension;
        protected void RatingBusiness_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
        {
            if (Session["id"] == null) return;
            _itemExtension = new BusinessItemExtension();
            var business = new BusinessItem(_itemExtension.SelectBusiness(BusinessId).Rows[0]);
            float total = business.BRating * business.BRater;
            total += float.Parse(e.Value);
            business.BRater++;
            business.BRating = (float)(total / business.BRater);
            _itemExtension.UpdateBusinessRating(business);
            lblRating.Text = string.Format("Thanks your rating for this business  : {0}", e.Value);
            RatingBusiness.CurrentRating = int.Parse(e.Value);
            UpWidget.Update();
        }

        public string GetLinkText()
        {
            var isWish = new WishListExtension().IsWish(UserId, BusinessId);
            return isWish ? "Remove From WishList" : "Add to WishList";
        }

        public Color GetLinkColor()
        {
            var isWish = new WishListExtension().IsWish(UserId, BusinessId);
            return isWish ? Color.Red : Color.Green;
        }

        private WishListExtension _listExtension;

        protected void lnkWishList_Click(object sender, EventArgs e)
        {
            _listExtension = new WishListExtension();
            bool isWish = new WishListExtension().IsWish(UserId, BusinessId);
            if (isWish)
            {
                _listExtension.RemoveFromWishList(UserId, BusinessId);
            }
            else
            {
                _listExtension.SetWishList(UserId, BusinessId);
            }
            InitializeComponent();
        }
    }
}